const Actions = require('./actions');
const DB = require('../../app/db');
const {fromJS ,OrderedMap} = require('immutable');
import {Toast} from 'saltui';
import Reflux from 'reflux';
import nattyStorage from 'natty-storage';

const storageUser = nattyStorage({
  type: 'localStorage',
  key: 'sskj-dingtalk-user',
});


module.exports = Reflux.createStore({
  listenables: [Actions],
  data: {
    rw: OrderedMap(),
    nRwsItems: OrderedMap(),
    uRwsItems: OrderedMap(),
    searchnRwsItems: OrderedMap(),
    searchuRwsItems: OrderedMap(),
    user: fromJS(storageUser),
    isRun: false,
    lastPage: false,
    activeUploadIndex: 0,
    currentPage: 1,
    pageSize: 6,
    ddconfig: {},
    activeIndex: 0,
    searchItems: [],
  },

  onRwGet: function (params) {

    let t = this;
    if (t.data.isRun == false) {
      if (params.operType == 'view') {
        t.data.rw = t.data.nRwsItems.get(params.listIndex).toJS();
      } else if (params.operType == 'search') {
        t.data.rw = t.data.searchnRwsItems.get(params.listIndex).toJS();
      }
    } else {
      if (params.operType == 'view') {
        t.data.rw = t.data.uRwsItems.get(params.listIndex).toJS();
      } else if (params.operType == 'search') {
        t.data.rw = t.data.searchuRwsItems.get(params.listIndex).toJS();
      }
    }



    t.updateComponent();
  },
  onSendMessageOA: function (params) {
    let t = this;
    DB.message.oa.send({
      userid: t.data.user.get('userid') && t.data.user.get('userid'),
      partyid: t.data.user.get('department') && t.data.user.get('department').join(','),
      title: params.get('empName') + '提交的' + params.get('txName'),
      text: '待办任务',
      forms: {'开始时间: ': params.get('kssj'), '结束时间: ': params.get('ywcsj')}

    }).then(
      function (content) {

      }, function (error) {
        Toast.hide(function () {
          alert('错误提示: ' + error.message);
        });
      });

  },

  onTabChange: function (params) {
    let t = this;
    t.data.activeUploadIndex = params.activeUploadIndex;
    t.data.lastPage = false;
    if (t.data.activeUploadIndex == 0) {
      t.data.isRun = false;
    } else {
      t.data.isRun = true;
    }
    t.updateComponent();
  },


  onRwInit: function () {
    let t = this;

    //DB.config.get(jsonConfig).then(
    //  function (content) {
    //    t.data.ddconfig = content;
    //    t.updateComponent();
    //  }, function (error) {
    //    console.log('获取配置失败' + JSON.stringify(error));
    //    alert('dd error: ' + JSON.stringify(error));
    //  });

    //111 this.onRwFetch({
    //   downOrUp: 'down',
    //   noRemove: false,
    // });

    //if (t.data.isRun == false) {
    //
    //  if (t.data.nRwsItems.count() == 0) {
    //    this.onRwFetch({
    //      downOrUp: 'down',
    //      noRemove: false,
    //    });
    //  }
    //}
    //
    //if (t.data.isRun == true) {
    //
    //  if (t.data.uRwsItems.count() == 0) {
    //    this.onRwFetch({
    //      downOrUp: 'down',
    //      noRemove: false,
    //    });
    //  }
    //}

    t.updateComponent();
  },

  onRwFetch: function (params, cb) {
    let t = this;

    if (params.downOrUp == 'down') {
      t.data.lastPage = false;
      t.data.currentPage = 1;
    }

    DB.jdrwmx.db.get.up({
      userid: t.data.user.get('userid') && t.data.user.get('userid'),
      page: t.data.currentPage,
      size: t.data.pageSize,
      sort: 'createdTime,DESC',
      downOrUp: params.downOrUp,
      isRun: t.data.isRun,
      lastPage: t.data.lastPage,
    }).then(
      function (content) {

        // t.data.searchItems = content;
        // t.updateComponent();
        console.info('数据',params.downOrUp);
        if (params.downOrUp == 'down') {
          if (content.length < t.data.pageSize) {
            t.data.lastPage = true;
          }
          if (t.data.isRun == false) {
            console.info('isRun_false',content);
            t.data.searchItems = content;
            console.info('searchItems',t.data.searchItems);
            t.updateComponent();
            t.data.nRwsItems = fromJS(content);
          } else {
            console.info('isRun_true',content);
            t.data.uRwsItems = fromJS(content);
          }
        } else {
          console.info('yewu到数据',content.length);
          if (content.length > 0) {
            if (t.data.nRwsItems.count() == 0) {
              if (t.data.isRun == false) {
                t.data.nRwsItems = fromJS(content);
              } else {
                t.data.uRwsItems = fromJS(content);
              }
            } else {
              if (t.data.isRun == false) {
                t.data.nRwsItems = t.data.nRwsItems.concat(fromJS(content));
              } else {
                t.data.uRwsItems = t.data.uRwsItems.concat(fromJS(content));
              }
            }
            if (content.length < t.data.pageSize) {
              t.data.lastPage = true;
            }
          } else {
            t.data.lastPage = true;
          }
        }
        t.data.currentPage = t.data.currentPage + 1;


        if (cb && typeof cb === 'function') {
          cb();
        }

        t.updateComponent();



      }, function (error) {
        Toast.hide(function () {
          alert('错误提示: ' + error.message);
          if (cb && typeof cb === 'function') {
            cb();
          }
        });
      }
    );
  },


  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
